var baseUrl = require('../../utils/urllink.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    store:[],
    user_money:0.00,
    store_money:0.00,
    page:1,
    page_size:10,
    visible:false,
    id:1,
    agent_type:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      agent_type: app.globalData.agent_type,
    })
    // 我的钱包
    my_wallet(that, app.globalData.user_id) 
    //账号的账单流水
    account_logs(that, app.globalData.user_id, 1, that.data.page_size) 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if(this.data.id == 2){
      //店铺流水
      store_account_logs(that, app.globalData.user_id, that.data.page, that.data.page_size) 
    }else{
      //账号的账单流水
      account_logs(that, app.globalData.user_id, that.data.page, that.data.page_size) 
    }
  },
  //点击推广收益
  usermoney:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      list:[],//清空数据
      id:id,
      page:1,
    })
    //账号的账单流水
    account_logs(that, app.globalData.user_id, 1, that.data.page_size) 
  },
  //点击销售收益
  storemoney:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      list: [],//清空数据
      id:id,
      page:1,
    })
    //店铺账单流水
    store_account_logs(that, app.globalData.user_id, 1, that.data.page_size) 
  },


})



//我的钱包
function my_wallet(that, user_id) {
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/my_wallet.html',

    data: {
      user_id: user_id,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      that.setData({
        user_money:res.data.user_money,
        store_money:res.data.store_money,
      })
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })


}

//账号的账单流水
function account_logs(that, user_id,page, page_size) {
  wx.showLoading({
    title: '加载中...',
  })
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/account_logs.html',

    data: {
      user_id: user_id,
      page:page,
      page_size:page_size,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if(res.data.code == -1){
        if(that.data.page == 1){
          //暂无相关数据
          that.setData({
            visible: false,
            id: 1,
          })
        }

      }else{
        if (that.data.page == 1){
          that.setData({
            list: res.data,
            visible: true, 
            id:1,
            page:page+1,
          })
        }else{
          var list = that.data.list;
          for(var i=0;i<res.data.length;i++){
            list.push(res.data[i]);
          }
          that.setData({
            list:list,
            visible:true,
            id: 1,
            page:page+1,
          })
        }

      }
      wx.hideLoading();

    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })


}

//店铺的账单流水
function store_account_logs(that, user_id,page,page_size) {
  wx.showLoading({
    title: '加载中...',
  })
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/store_account_logs.html',

    data: {
      user_id: user_id,
      page:page,
      page_size:page_size,
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      if (res.data.code == -1) {
        if (that.data.page == 1) {
          //暂无相关数据
          that.setData({
            visible: false,
            id: 2,
          })
        }

      } else {
        if (that.data.page == 1) {
          that.setData({
            storelist: res.data,
            visible: true,
            id: 2,
            page: page + 1,
          })
        } else {
          var storelist = that.data.list;
          for (var i = 0; i < res.data.length; i++) {
            storelist.push(res.data[i]);
          }
          that.setData({
            storelist: storelist,
            visible: true,
            id: 2,
            page: page + 1,
          })
        }

      }
      wx.hideLoading();
    },
    fail: function () {
    },
    complete: function () {
      // complete
    }
  })


}